package network.exercise.exercise2;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-04-05
 * Time:19:50
 */
public class Receiver {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(8888);
        byte[] buf = new byte[1024 * 54];
        DatagramPacket packet = new DatagramPacket(buf, buf.length);
        socket.receive(packet);
        String data = new String(packet.getData(),0,packet.getLength());
//        System.out.println(data);
        if (data.equals("What are the Four Great Classical Novels?")) {
            byte[] bytes = "The four great classics are Dream of the Red Chamber, Journey to the West, Romance of the Three Kingdoms, and Water Margin".getBytes();
            packet = new DatagramPacket(bytes, 0, bytes.length, InetAddress.getLocalHost(), 9999);
        } else {
            byte[] bytes = "What?".getBytes();
            packet = new DatagramPacket(bytes, 0, bytes.length, InetAddress.getLocalHost(), 9999);
        }
        socket.send(packet);
        socket.close();
    }
}
